/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: UsersServiceImpl.java
 * Date: 01/03/2011
 */

package com.ingenius.sau.server.service;

import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.service.InstrumentsService;
import com.ingenius.sau.server.persistence.DAOFactory;
import com.ingenius.sau.server.persistence.DocumentDAO;
import com.ingenius.sau.server.persistence.QuestionDAO;
import com.ingenius.sau.server.persistence.ResponseDAO;
import com.ingenius.sau.server.persistence.SelfEvaluationDAO;
import com.ingenius.sau.server.persistence.jdo.JdoDAOFactory;
import com.ingenius.sau.shared.model.core.SelfEvaluation;
import com.ingenius.sau.shared.model.instruments.Document;
import com.ingenius.sau.shared.model.instruments.Question;
import com.ingenius.sau.shared.model.instruments.Response;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>UsersServiceImpl</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
@SuppressWarnings("serial")
public class InstrumentsServiceImpl extends AbstractService implements
		InstrumentsService {

	private DAOFactory daoFactory;
	private DocumentDAO documentDAO;
	private QuestionDAO questionDAO;
	private ResponseDAO responseDAO;
	private SelfEvaluationDAO processDAO;

	public InstrumentsServiceImpl() {
		daoFactory = JdoDAOFactory.getInstance();
		documentDAO = daoFactory.createDocumentDAO();
		questionDAO = daoFactory.createQuestionDAO();
		responseDAO = daoFactory.createResponseDAO();
		processDAO = daoFactory.createSelfEvaluationDAO();
	}

	@Override
	public Boolean saveOrUpdateDocument(Document document) {
		String name = null, url = null;
		try {
			name = (String) getSession().getAttribute(
					Constants.COOKIE_FILE_NAME);
			url = (String) getSession().getAttribute(Constants.COOKIE_FILE_URL);
		} catch (Exception e) {

		}
		if (name != null && url != null) {
			document.setAttachmentName(name);
			document.setAttachmentUrl(url);
			getSession().setAttribute(Constants.COOKIE_FILE_NAME, null);
			getSession().setAttribute(Constants.COOKIE_FILE_URL, null);
		}
		if (document.getId() == null) {
			return documentDAO.save(document);
		}
		return documentDAO.update(document);
	}

	@Override
	public Boolean deleteDocument(String id) {
		return documentDAO.delete(id);
	}

	@Override
	public Document getDocument(String id) {
		return documentDAO.get(id);
	}

	@Override
	public PagingLoadResult<Document> findDocuments(List<Filter> filters,
			BasePagingLoadConfig config) {

		return super.find(documentDAO, filters, config);
	}

	@Override
	public Boolean saveOrUpdateQuestion(Question question) {
		if (question.getId() == null) {
			return questionDAO.save(question);
		}
		return questionDAO.update(question);
	}

	@Override
	public Boolean deleteQuestion(String id) {
		return questionDAO.delete(id);
	}

	@Override
	public Question getQuestion(String id) {
		return questionDAO.get(id);
	}

	@Override
	public PagingLoadResult<Question> findQuestions(List<Filter> filters,
			BasePagingLoadConfig config) {

		return super.find(questionDAO, filters, config);
	}

	@Override
	public Boolean saveOrUpdateResponse(Response response) {
		if (response.getId() == null) {
			responseDAO.save(response);
		}
		return responseDAO.update(response);
	}

	@Override
	public Boolean deleteResponse(String id) {
		return responseDAO.delete(id);
	}

	@Override
	public Response getResponse(String id) {
		return responseDAO.get(id);
	}

	@Override
	public PagingLoadResult<Response> findResponses(List<Filter> filters,
			BasePagingLoadConfig config) {

		return super.find(responseDAO, filters, config);
	}

	@Override
	public List<SelfEvaluation> loadAllProcesses() {
		return super.find(processDAO, null, null).getData();
	}

}
